package com.niit.galuser.controller;

import com.niit.entity.User;
import com.niit.galuser.client.GameClient;
import com.niit.galuser.service.impl.UserServiceImpl;
import com.niit.utils.CommonResult;
import org.apache.http.HttpResponse;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.HttpCookie;

@RestController
@RequestMapping("login")
public class LoginController {
    @Resource
    private UserServiceImpl userService;
    @Resource
    private GameClient gameClient;
    @PostMapping("bypasswd")
    public CommonResult loginByPass(@RequestBody User user,HttpSession session){
        User user1 =userService.findUserByMail(user.getEmail());
        if (user1==null)
            return new CommonResult(401,"用户不存在");
        else if (!user.getPassword().equals(user1.getPassword()))
            return new CommonResult(401,"密码错误");
        session.setAttribute("user",user1);
        return new CommonResult(200,"验证成功");
    }

    @PostMapping("bymail")
    public CommonResult loginByMail(HttpSession session, @RequestBody User user){
        String addr = user.getEmail();
        String code = user.getCode();
        User user1 = userService.findUserByMail(addr);
        if (user1==null)
            return new CommonResult(401,"用户不存在");
        else if (!code.equals(session.getAttribute(addr)))
            return new CommonResult(401,"验证码错误");
        session.setAttribute("user",user1);
        return new CommonResult(200,"验证成功");
    }
}
